<?php if ($totalPages > 1): ?>
    <div class="card-footer d-flex align-items-center">
        <ul class="pagination m-0 ms-auto">
            <!-- 首页按钮 -->
            <li class="page-item <?= $page == 1 ? 'disabled' : '' ?>">
                <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => 1])) ?>">首页</a>
            </li>
            <!-- 上一页按钮 -->
            <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => $page - 1])) ?>">上一页</a>
            </li>
            <?php
            // 动态生成页码
            $maxVisiblePages = 9; // 显示的页码数量
            $halfVisiblePages = floor($maxVisiblePages / 2);

            $start = max(1, $page - $halfVisiblePages);
            $end = min($totalPages, $start + $maxVisiblePages - 1);

            // 调整起始页，确保显示足够的页码
            if ($end - $start + 1 < $maxVisiblePages) {
                $start = max(1, $end - $maxVisiblePages + 1);
            }

            // 显示前面的省略号
            if ($start > 1) {
                echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
            }

            for ($i = $start; $i <= $end; $i++): ?>
                <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                    <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => $i])) ?>">
                        <?= $i ?>
                    </a>
                </li>
            <?php endfor;

            // 显示后面的省略号
            if ($end < $totalPages) {
                echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
            }
            ?>
            <!-- 下一页按钮 -->
            <li class="page-item <?= $page >= $totalPages ? 'disabled' : '' ?>">
                <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => $page + 1])) ?>">下一页</a>
            </li>
            <!-- 末页按钮 -->
            <li class="page-item <?= $page == $totalPages ? 'disabled' : '' ?>">
                <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => $totalPages])) ?>">末页</a>
            </li>
        </ul>
    </div>
<?php endif; ?>
<!-- Path: page.php -->